<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<html>
<head>
	<title>用户管理</title>
	
	<script>
		$(document).ready(function() {
			//聚焦第一个输入框
			$("#name").focus();
			//为inputForm注册validate函数
			$("#inputForm").validate();
			$("#addcs_btn").click(addcs_btn_click);
			$("#addcs_submit_btn").click(addcs_submit_btn_click);
		});
		function addcs_btn_click(){
			$.post(
				'${ctx}/profile/getEnabledProfile',
				{exludeIdList:filter_profiles()},
				function(data) {
					$("#profile_self").empty();
					$.each(data, function(key, value){      
						$("#profile_self").append("<option value='" + key + "'>" + value +"</option>");
					});
					$("#profiles_alert").show();
			});
		}
		function addcs_submit_btn_click(){
			var checkProfileValue=$("#profile_self").val(),
				checkProfileText=$("#profile_self").find("option:selected").text(),
			 	len = $("#profiles tr").length,
			 	index = len - 1,
			 	hiddenInput = '<td style="display:none"><input type="text" name="profiles['+ index + '].id" value="'+checkProfileValue+'"/></td>';
	        var rowTemplate = '<tr id="cc_'+ index +'"><td>'+checkProfileText+'</td><td><a href="#" onclick="delRow('+index+')">删除</a></td>'+hiddenInput+'</tr>';
	        var tableHtml = $("#profiles tbody").html();
	        tableHtml += rowTemplate;
	        $("#profiles tbody").html(tableHtml);
	        $('#profiles_alert').hide();
		}
	   	function delRow(row_id){
	   		//删除当前行
			$("tr[id='cc_"+row_id+"']").remove();
		}
	   	function filter_profiles(){
	   		var csArray = $('input[name="^profiles"]'),
	   			ids = new Array();
	   		if(csArray){
		   		$.each(csArray, function(i, cs){
					ids.push(cs.value);
				});
	   		}
			return ids;
	   	}
	</script>
</head>

<body>
	<form id="inputUserForm" action="${ctx}/admin/user/${action}" method="post" class="form-horizontal">
		<input type="hidden" name="id" value="${user.id}"/>
		<fieldset>
			<legend><small>用户管理</small></legend>
			<div class="control-group">
				<label class="control-label">登录名:</label>
				<div class="controls">
					<input type="text" id="loginName" name="loginName" value="${user.loginName}" class="input-large required" />
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">用户名:</label>
				<div class="controls">
					<input type="text" id="name" name="name" value="${user.name}" class="input-large required"/>
				</div>
			</div>
			<div class="control-group">
				<label for="plainPassword" class="control-label">密码:</label>
				<div class="controls">
					<input type="password" id="plainPassword" name="plainPassword" class="input-large required" placeholder="请输入密码" autocomplete="off"/>
				</div>
			</div>
			<div class="control-group">
				<label for="confirmPassword" class="control-label">确认密码:</label>
				<div class="controls">
					<input type="password" id="confirmPassword" name="confirmPassword" class="input-large required" equalTo="#plainPassword" placeholder="请再次输入密码" autocomplete="off"/>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">注册日期:</label>
				<div class="controls">
					<span class="help-inline" style="padding:5px 0px"><fmt:formatDate value="${user.registerDate}" pattern="yyyy年MM月dd日  HH时mm分ss秒" /></span>
				</div>
			</div>
			
			<div class="control-group">
				<table id="profiles" class="table table-striped table-bordered table-condensed">
					<thead><tr><th>彩页</th><th>操作</th></tr></thead>
					<tbody>
						<c:forEach var="profile" items="${user.profiles}" varStatus="loopStatus">
								<tr id="cc_${loopStatus.index}">
									<td>${profile.name}</td>
									<td><a href="#" onclick="delRow(${loopStatus.index})">删除</a></td>
									<td style="display:none">
										<spring:bind path="user.profiles[${loopStatus.index}]">
											<input type="text" name="${status.expression}.id" value="${status.value.id}" />
										</spring:bind>
									</td>
								</tr>
							
						</c:forEach>
					</tbody>
				</table>
			</div>
			<div id="profiles_alert" class="alert alert-info" style="display:none">
				<div class="control-group">
					<label for="profile_self" class="control-label">彩页:</label>
					<div class="controls">
						<select id="profile_self"></select>
					</div>
				</div>
				<div class="form-actions">
					<input id="addcs_submit_btn" class="btn btn-primary" type="button" value="确定"/>&nbsp;
					<input id="addcs_cancel_btn" class="btn" type="button" value="取消" onclick="$('#profiles_alert').hide()"/>
				</div>
			</div>
			<div class="form-actions">
				<input id="submit_btn" class="btn btn-primary" type="submit" value="提交"/>&nbsp;	
				<input id="cancel_btn" class="btn" type="button" value="返回" onclick="history.back()"/>
				<input id="addcs_btn" class="btn" type="button" value="新增彩页关系"/>
			</div>
		</fieldset>
	</form>
</body>
</html>
